package net.example.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.esbasic.loggers.FileLogger;
import com.oraycn.esframework.EsfGlobalUtil;
import com.oraycn.esframework.core.IRapidPassiveEngine;
import com.oraycn.omcs.OmcsGlobalUtil;

import net.example.ggtalk.R;
import net.example.manager.ClientGlobalCache;
import net.example.manager.GGApplication;
import net.example.utils.ConfigUtils;
import net.example.utils.LogHelper;
import net.example.utils.SDKUtil;
import net.example.utils.SharePreferenceUtil;
import net.example.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;

import static net.example.utils.ConfigUtils.SDLogSavePath;


/**
 * 开屏页
 * 
 */
public class SplashActivity extends BaseActivity {
    private RelativeLayout rootLayout;
    private TextView versionText;

    private static final int sleepTime = 2000;
private SharePreferenceUtil sharePreferenceUtil;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_splash);
        byte maxUserIDLen=20;//设置用户长度
        EsfGlobalUtil.setMaxLengthOfUserId(maxUserIDLen);
//        EsfGlobalUtil.setU(maxUserIDLen);
        OmcsGlobalUtil.setMaxLengthOfUserId(maxUserIDLen);
        sharePreferenceUtil = new SharePreferenceUtil(this,
				ConfigUtils.appSharePreferenceName);
//        rootLayout = (RelativeLayout) findViewById(R.id.splash_root);
//        versionText = (TextView) findViewById(R.id.tv_version);

//        versionText.setText("V."+getVersion());
//        AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
//        animation.setDuration(1500);
//        rootLayout.startAnimation(animation);
        int version = SDKUtil.getAndroidSDKVersion();//获取SDK版本号
        LogHelper.e("获取系统版本号：" + version);

    }

    @Override
    protected void onStart() {
        try {
            super.onStart();
            //点击通知消息进入
            String talkingUserID = getIntent().getStringExtra("TalkingUserID");
            IRapidPassiveEngine engin = ClientGlobalCache.getInstance().getRapidPassiveEngine();
            Uri uri = getIntent().getData();
            if (engin != null && engin.connected()) {
                if (!StringHelper.isNullOrEmpty(talkingUserID)) {
                    this.moveMainPage();
                    return;
                } else {
                    engin.close();
                }
            }
            this.getPermission();
        } catch (Exception ex) {
            ex.printStackTrace();
            ToastUtils.showLong(SplashActivity.this, ex.getMessage());
        }
    }

    /**
     * 跳转到主页面
     */
    private  void moveMainPage()
    {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.putExtra("tabID", R.id.btn_conversation);
        startActivity(intent);
    }

    /**
     * 跳转到登录页
     * */
    private void moveLoginPage()
    {
        GGApplication.getInstance().setFileLogger(new FileLogger(SDLogSavePath+"oraytalk.txt"));
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                }
                Intent intent = new Intent(SplashActivity.this,
                        LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }).start();
    }

    /**
     * 获取当前应用程序的版本号
     */
    private String getVersion() {
        String st = getResources().getString(R.string.Version_number_is_wrong);
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packinfo = pm.getPackageInfo(getPackageName(), 0);
            String version = packinfo.versionName;
            LogHelper.e(version);
            sharePreferenceUtil.setVersion(version);
            return version;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return st;
        }
    }

    /**
     * 获取权限
     * */
    private void getPermission()
    {
        List<PermissionItem> permissionItems = new ArrayList<PermissionItem>();
        permissionItems.add(new PermissionItem(Manifest.permission.CAMERA,"相机",R.drawable.permission_ic_camera));
        permissionItems.add(new PermissionItem(Manifest.permission.RECORD_AUDIO,"麦克风",R.drawable.permission_ic_micro_phone));
        permissionItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE,"存储",R.drawable.permission_ic_storage));
        permissionItems.add(new PermissionItem(Manifest.permission.READ_EXTERNAL_STORAGE,"",0));
        try {
            HiPermission.create(SplashActivity.this)
                    .title("欢迎访问"+getString(R.string.app_name))
                    .permissions(permissionItems)
                    .checkMutiPermission(new PermissionCallback() {

                        String TAG=getString(R.string.app_name);
                        @Override
                        public void onClose() {
                            Log.i(TAG, "onClose");
                            // showToast(getString(R.string.permission_on_close));
                        }

                        @Override
                        public void onFinish() {
                            Log.i(TAG, "onFinish");
                            ConfigUtils.initialize();
                            moveLoginPage();
                            // showToast(getString(R.string.permission_completed));
                        }

                        @Override
                        public void onDeny(String permission, int position) {
                            Log.i(TAG, "onDeny- permission："+permission+"   position:"+position);
                        }

                        @Override
                        public void onGuarantee(String permission, int position) {
                            Log.i(TAG, "onGuarantee");
                        }
                    });
        }catch (Exception e)
        {
            e.printStackTrace();
            ToastUtils.showLong(SplashActivity.this,e.getMessage());
        }

    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

}
